package com.zxx.casserver.base.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis基础操作工具类
 *
 * @author zhoukai
 * @date 2020/9/2 11:48
 */
@Component
public class RedisService {

    private final StringRedisTemplate stringRedisTemplate;

    private final RedisTemplate redisTemplate;

    @Value("${user.redis.expireTime}")
    private long expiryTime;

    public RedisService(StringRedisTemplate stringRedisTemplate, RedisTemplate redisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.redisTemplate = redisTemplate;
    }

    public boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }


    public void increment(String key, Integer value) {
        stringRedisTemplate.boundValueOps(key).increment(value);
    }


    public void expire(String key, Long expiryTime) {
        stringRedisTemplate.expire(key, expiryTime, TimeUnit.SECONDS);
    }


    public void set(String key, String value, long expiryTime) {
        stringRedisTemplate.opsForValue().set(key, value, expiryTime, TimeUnit.SECONDS);
    }

    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value, expiryTime, TimeUnit.SECONDS);
    }


    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public List<String> multiGet(List<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

    public boolean delete(String key) {
        return stringRedisTemplate.opsForValue().getOperations().delete(key);
    }

    public void hSetAll(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }


    public void hSetAll(String key, Map<String, Object> map, long expiryTime) {
        redisTemplate.opsForHash().putAll(key, map);
        redisTemplate.expire(key, expiryTime, TimeUnit.SECONDS);
    }


    public void hSet(String key, String field, String value, long expiryTime) {
        redisTemplate.opsForHash().put(key, field, value);
        redisTemplate.expire(key, expiryTime, TimeUnit.SECONDS);
    }

    public Long hSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    public void hDelKey(String key, String field) {
        redisTemplate.opsForHash().delete(key, field);
    }


    public String hGet(String key, String field) {
        Object val = redisTemplate.opsForHash().get(key, field);
        return val == null ? null : val.toString();
    }

    public Map<String, Object> hGetAll(String key) {
        return redisTemplate.opsForHash().entries(key);
    }


    public Long leftPush(String key, String value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }


    public Long leftPushAll(String key, List<String> values) {
        return redisTemplate.opsForList().leftPushAll(key, values);
    }

    public String rightPop(String key) {
        return String.valueOf(redisTemplate.opsForList().rightPop(key));
    }


    public Long size(String key) {
        return redisTemplate.opsForList().size(key);
    }


    public Long increment(String key, Long value) {
        return stringRedisTemplate.opsForValue().increment(key, value);
    }

    /**
     * 获取列表的子列表：LRANGE start end。返回第 start 个到第 end 个元素的列表。
     * 包含 start 和 end。支持负数索引。-1 表示最后一个元素，-2 表示倒数第二个元素
     *
     * @param key
     * @param start
     * @param end
     * @author zhoukai
     * @date 2020/9/7 16:09
     */
    public List<String> lRange(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 保留片段，删除其它：LTRIM key start end。保留 start 到 end 之间的所有元素，含 start 和 end。其他全部删除
     *
     * @param key
     * @param start
     * @param end
     * @author zhoukai
     * @date 2020/9/7 16:09
     */
    public void lTrim(String key, long start, long end) {
        redisTemplate.opsForList().trim(key, start, end);
    }


    /**
     * 删除list中指定key 对应count数量 值为value的数据
     *
     * @param key
     * @param count
     * @param value
     * @author zhoukai
     * @date 2020/9/7 16:07
     */
    public void lremove(String key, long count, String value) {
        redisTemplate.opsForList().remove(key, count, value);
    }


    /**
     * 批量向右弹出队列中的元素
     *
     * @param key
     * @param batchSize
     * @author zhoukai
     * @date 2020/9/7 16:07
     */
    public List<String> batchRightPop(String key, int batchSize) {
        List<String> resultList = (List<String>) redisTemplate.executePipelined(
                (RedisCallback<Object>) connection -> {
                    for (int i = 0; i < batchSize; i++) {
                        connection.rPop(key.getBytes());
                    }
                    return null;
                }
        );
        if (ObjectUtils.isEmpty(resultList)) {
            return null;
        }
        List<String> newResult = new ArrayList<>(batchSize + 12);
        for (String str : resultList) {
            if (!StringUtils.isEmpty(str)) {
                newResult.add(str);
            }
        }
        return newResult;
    }


}
